package com.leased.filter;

import com.leased.core.ActionContext;
import com.leased.core.AuthUser;
import com.leased.utils.CookieUtils;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by shencx on 2017/3/24.
 */
public class ActionContextBuildFilter implements Filter {

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {


        //暂时先加着
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        HttpSession session  = httpServletRequest.getSession();
        String path = httpServletRequest.getRequestURI();
        response.setCharacterEncoding("UTF-8");
        //设置 ActionContext
        ActionContext.setActionContext(session.getServletContext(),
                (HttpServletRequest)request,
                (HttpServletResponse) response);

//        Cookie[] cookies =  httpServletRequest.getCookies();
//        for (Cookie cookie : cookies){
//            if (!cookie.getName().equals("LEASED_USER") && (path.indexOf("/js") > -1 || path.indexOf("/view") > -1)){
//                httpServletResponse.sendRedirect("../login.jsp");
//                return;
//            }else if (!cookie.getName().equals("LEASED_USER") && (path.indexOf("/main.jsp") > -1 || path.indexOf("top.jsp") > -1)){
//                httpServletResponse.sendRedirect("login.jsp");
//                return;
//            }
//        }
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

}
